package com.youlai.boot.system.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.ExamPapersMapper;
import com.youlai.boot.system.service.ExamPapersService;
import com.youlai.boot.system.model.entity.ExamPapers;
import com.youlai.boot.system.model.form.ExamPapersForm;
import com.youlai.boot.system.model.query.ExamPapersQuery;
import com.youlai.boot.system.model.vo.ExamPapersVO;
import com.youlai.boot.system.converter.ExamPapersConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 试卷题目服务实现类
 *
 * @author youlaitech
 * @since 2025-07-03 10:57
 */
@Service
@RequiredArgsConstructor
public class ExamPapersServiceImpl extends ServiceImpl<ExamPapersMapper, ExamPapers> implements ExamPapersService {

    private final ExamPapersConverter examPapersConverter;

    /**
    * 获取试卷题目分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<ExamPapersVO>} 试卷题目分页列表
    */
    @Override
    public IPage<ExamPapersVO> getExamPapersPage(ExamPapersQuery queryParams) {
        Page<ExamPapersVO> pageVO = this.baseMapper.getExamPapersPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取试卷题目表单数据
     *
     * @param id 试卷题目ID
     * @return 试卷题目表单数据
     */
    @Override
    public ExamPapersForm getExamPapersFormData(Long id) {
        ExamPapers entity = this.getById(id);
        return examPapersConverter.toForm(entity);
    }
    
    /**
     * 新增试卷题目
     *
     * @param formData 试卷题目表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveExamPapers(ExamPapersForm formData) {
        ExamPapers entity = examPapersConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新试卷题目
     *
     * @param id   试卷题目ID
     * @param formData 试卷题目表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateExamPapers(Long id,ExamPapersForm formData) {
        ExamPapers entity = examPapersConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除试卷题目
     *
     * @param ids 试卷题目ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteExamPaperss(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的试卷题目数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
